﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;

namespace PersonalAlbum 
{
      public class PartialAttribute : ActionFilterAttribute  
     {  
         private string _partialClassName;  
   
         public PartialAttribute(string partialClassName)  
         {  
             _partialClassName = partialClassName;  
         }  
   
         public override void OnActionExecuting(ActionExecutingContext filterContext)  
         {  
             var viewData = (filterContext.Controller as Controller).ViewData;  
             ExecutePartial(_partialClassName, viewData);  
         }  
   
         private void ExecutePartial(string partialName, ViewDataDictionary viewData)  
         {
             // Get partial type  
             var partialType = Type.GetType(partialName, true, true);  
             var partial = Activator.CreateInstance(partialType);  
   
             // Execute all public methods  
             var methods = partialType.GetMethods();  
             foreach (MethodInfo method in methods)  
             {  
                 var pams = method.GetParameters();  
                 if (pams.Length > 0 && pams[0].ParameterType == typeof(ViewDataDictionary))  
                     method.Invoke(partial, new object[] { viewData });  
   
             }  
         }  
   
     }  
}
